import os
import random
import tkinter
import sys

"""
操作如下：
点击选择链接内容，这将打开网页并尝试自动登录，
点击 隐藏，会刷新配置并缩小显示范围，点击 恢复，根据刷新后配置恢复
双击右键关闭
"""


def random_color_light():
    x1 = ['B', 'A', '9', '8', 'C', 'D', 'E', 'F']
    x2 = ['0', '1', '2', '3', '4', '5', '6', '7']
    s = '#'
    for i in range(6):
        s += random.choice(x1)
        s += random.choice(x2)
    return s


def random_color():
    x1 = ['2', '4', '5', '6', '3', '4', '2', '1']
    x2 = ['0', '1', '2', '3', '4', '5', '6', '7']
    s = '#'
    for i in range(6):
        s += random.choice(x1)
        s += random.choice(x2)
    return s


def shutdown():
    os.system('shutdown -s -t 5')

class DragWindow:
    def __init__(self):
        # 获取命令行参数
        args = sys.argv[1:]
        # 解析-c和-t后面的值
        self.text = '晚安，注意休息哦~'
        self.delay = 300
        for i in range(len(args)):
            if args[i] == '-c':
                self.text = args[i + 1]
            elif args[i] == '-t':
                self.delay = int(args[i + 1])
        self.run()

    def new_window(self):
        self.root = tkinter.Tk()
        self.flag = 0

        self.x, self.y = 0, 0
        self.root.geometry(f"-10+10")  # 置初始位置为屏幕外

        # 设置隐藏窗口标题栏和任务栏图标
        self.root.overrideredirect(True)
        # 窗口透明度60%
        self.root.attributes("-alpha", 0.65)
        # 设定背景颜色，样式
        self.root.configure(bg=random_color(), pady=4, padx=4)

        # 单击事件
        self.root.bind("<Button-1>", self.close)
        # 双击事件
        self.root.bind("<Double-Button-3>", self.close)

        # 去除标签栏及页头
        self.root.overrideredirect(True)
        # 将页面置为最顶层
        self.root.attributes('-topmost', True)

        color_label = random_color_light()
        label = tkinter.Label(width=1000, height=200, pady=1, padx=1, bg=color_label)
        label.pack()

        tkinter.Message(label, text=self.text, font=('Arial', 50, 'bold'), fg=random_color(), bg=color_label,
                        width=1000).pack()
        tkinter.Message(label, text='(双击右键或单击都会取消关机进程，否则它会在%d分钟后自作主张~)' % (self.delay / 60),
                        font=('Arial', 12, 'bold'), fg=random_color(), bg=color_label, width=1000).pack()

    def run(self):
        self.new_window()
        self.root.after(self.delay*1000, self.root.destroy)
        self.root.mainloop()

    def close(self, *args):
        self.root.destroy()
        self.flag = 1

    def __del__(self):
        if self.flag == 0:
            shutdown()


if __name__ == "__main__":
    window = DragWindow()
